package com.ruoyi.web.controller.questionBank;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.vo.PageResult;
import com.ruoyi.questionBank.domain.Question;
import com.ruoyi.questionBank.service.IQuestionService;
import com.ruoyi.questionBank.vo.QuestionListVO;
import org.apache.poi.util.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/questionBank/question")
public class QuestionController extends BaseController {
    @Autowired
    private IQuestionService questionService;

    @PreAuthorize("@ss.hasPermi('questionBank:query')")
    @RequestMapping("/list")
    public TableDataInfo list(QuestionListVO listVO){
        PageResult<Question> pageResult = questionService.get(listVO);
        return getDataTable(pageResult.convertToPage());
    }

    @PreAuthorize("@ss.hasPermi('questionBank:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Question question){
        long uid = getLoginUser().getUserId();
        question.setCreatePerson(uid);
        question.setEnable(true);
        questionService.add(question);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('questionBank:update')")
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Question question){
        questionService.update(question);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('questionBank:del')")
    @PostMapping("/del")
    public AjaxResult del(@RequestBody Integer qid){
        questionService.del(qid);
        return AjaxResult.success();
    }

    @PreAuthorize("@ss.hasPermi('questionBank:import')")
    @RequestMapping("/import")
    public AjaxResult importByFile(MultipartFile file) throws IOException {
        questionService.importByFile(getLoginUser().getUserId() ,file.getInputStream());
        return AjaxResult.success();
    }

    @RequestMapping("/importTemplate")
    public void downloadTemplate(HttpServletResponse response) throws Exception{
        File file = ResourceUtils.getFile("classpath:question_import.xlsx");
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        response.setHeader("Content-Disposition", "attachment;fileName=题目导入模板.xlsx");

        response.getOutputStream().write(FileUtil.readBytes(file));
    }

}
